Let’s define some vectors which can be used for demonstrations:
manyNumbers <- sample( 1:1000, 20 )
manyNumbers
[1] 633 174 212 623 176 527 845 241 462 570 342 644 83 205 969 676 985 50 512 726
manyNumbersWithNA <- sample( c( NA, NA, NA, manyNumbers ) )
manyNumbersWithNA
[1] 462 241 83 985 676 176 633 205 512 726 644 174 623 NA 527 NA 969 845 342 NA 570 50 212
duplicatedNumbers <- sample( 1:5, 10, replace = TRUE )
duplicatedNumbers
[1] 5 3 1 1 5 4 2 2 5 1
letters
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u" "v" "w" "x" "y" "z"
LETTERS
[1] "A" "B" "C" "D" "E" "F" "G" "H" "I" "J" "K" "L" "M" "N" "O" "P" "Q" "R" "S" "T" "U" "V" "W" "X" "Y" "Z"
mixedLetters <- c( sample( letters, 5 ), sample( LETTERS, 5 ) )
mixedLetters
[1] "m" "y" "z" "q" "w" "J" "F" "K" "L" "P"
manyNumbersWithNA instead of manyNumbers.all( manyNumbers <= 1000 )
[1] TRUE
all( manyNumbers <= 500 )
[1] FALSE
any( manyNumbers > 1000 )
[1] FALSE
any( manyNumbers > 500 )
[1] TRUE
all( !is.na( manyNumbers ) )
[1] TRUE
any( is.na( manyNumbers ) )
[1] FALSE
Input: logical vector Output: vector of numbers (positions)
which( manyNumbers > 900 )
[1] 15 17
which( manyNumbersWithNA > 900 )
[1] 4 17
which( is.na( manyNumbersWithNA ) )
[1] 14 16 20
manyNumbers[ manyNumbers > 900 ] # indexing by logical vector
[1] 969 985
manyNumbers[ which( manyNumbers > 900 ) ] # indexing by positions
[1] 969 985
somePositions <- which( manyNumbers > 900 )
manyNumbers[ somePositions ]
[1] 969 985
"A" %in% LETTERS
[1] TRUE
c( "X", "Y", "Z" ) %in% LETTERS
[1] TRUE TRUE TRUE
all( c( "X", "Y", "Z" ) %in% LETTERS )
[1] TRUE
all( mixedLetters %in% LETTERS )
[1] FALSE
any( mixedLetters %in% LETTERS )
[1] TRUE
mixedLetters[ mixedLetters %in% LETTERS ]
[1] "J" "F" "K" "L" "P"
mixedLetters[ !( mixedLetters %in% LETTERS ) ]
[1] "m" "y" "z" "q" "w"
manyNumbers %in% 300:600
[1] FALSE FALSE FALSE FALSE FALSE TRUE FALSE FALSE TRUE TRUE TRUE FALSE FALSE FALSE FALSE FALSE FALSE
[18] FALSE TRUE FALSE
which( manyNumbers %in% 300:600 )
[1] 6 9 10 11 19
sum( manyNumbers %in% 300:600 )
[1] 5
NAsif_else( manyNumbersWithNA >= 500, "large", "small" )
[1] "small" "small" "small" "large" "large" "small" "large" "small" "large" "large" "large" "small" "large"
[14] NA "large" NA "large" "large" "small" NA "large" "small" "small"
if_else( manyNumbersWithNA >= 500, "large", "small", "UNKNOWN" )
[1] "small" "small" "small" "large" "large" "small" "large" "small" "large" "large"
[11] "large" "small" "large" "UNKNOWN" "large" "UNKNOWN" "large" "large" "small" "UNKNOWN"
[21] "large" "small" "small"
# here integer 0L is needed instead of real 0.0
# manyNumbersWithNA contains integer numbers and the method complains
if_else( manyNumbersWithNA >= 500, manyNumbersWithNA, 0L )
[1] 0 0 0 985 676 0 633 0 512 726 644 0 623 NA 527 NA 969 845 0 NA 570 0 0
unique( duplicatedNumbers )
[1] 5 3 1 4 2
unique( c( NA, duplicatedNumbers, NA ) )
[1] NA 5 3 1 4 2
duplicated( duplicatedNumbers )
[1] FALSE FALSE FALSE TRUE TRUE FALSE FALSE TRUE TRUE TRUE
which.max( manyNumbersWithNA )
[1] 4
manyNumbersWithNA[ which.max( manyNumbersWithNA ) ]
[1] 985
which.min( manyNumbersWithNA )
[1] 22
manyNumbersWithNA[ which.min( manyNumbersWithNA ) ]
[1] 50
range( manyNumbersWithNA, na.rm = TRUE )
[1] 50 985
manyNumbersWithNA
[1] 462 241 83 985 676 176 633 205 512 726 644 174 623 NA 527 NA 969 845 342 NA 570 50 212
sort( manyNumbersWithNA )
[1] 50 83 174 176 205 212 241 342 462 512 527 570 623 633 644 676 726 845 969 985
sort( manyNumbersWithNA, na.last = TRUE )
[1] 50 83 174 176 205 212 241 342 462 512 527 570 623 633 644 676 726 845 969 985 NA NA NA
sort( manyNumbersWithNA, na.last = TRUE, decreasing = TRUE )
[1] 985 969 845 726 676 644 633 623 570 527 512 462 342 241 212 205 176 174 83 50 NA NA NA
manyNumbersWithNA[1:5]
[1] 462 241 83 985 676
order( manyNumbersWithNA[1:5] )
[1] 3 2 1 5 4
rank( manyNumbersWithNA[1:5] )
[1] 3 2 1 5 4
sort( mixedLetters )
[1] "F" "J" "K" "L" "m" "P" "q" "w" "y" "z"
manyDuplicates <- sample( 10:15, 10, replace = TRUE )
rank( manyDuplicates )
[1] 2.0 5.0 7.5 5.0 9.5 5.0 2.0 9.5 2.0 7.5
rank( manyDuplicates, ties.method = "min" )
[1] 1 4 7 4 9 4 1 9 1 7
rank( manyDuplicates, ties.method = "random" )
[1] 1 6 8 5 9 4 2 10 3 7
v <- c( -1, -0.5, 0, 0.5, 1, rnorm( 10 ) )
v
[1] -1.00000000 -0.50000000 0.00000000 0.50000000 1.00000000 -0.55700252 0.28980461 0.02789515
[9] 0.27340777 0.43334730 -0.28217503 -0.30836190 -0.48783588 -0.86480465 0.88774686
round( v, 0 )
[1] -1 0 0 0 1 -1 0 0 0 0 0 0 0 -1 1
round( v, 1 )
[1] -1.0 -0.5 0.0 0.5 1.0 -0.6 0.3 0.0 0.3 0.4 -0.3 -0.3 -0.5 -0.9 0.9
round( v, 2 )
[1] -1.00 -0.50 0.00 0.50 1.00 -0.56 0.29 0.03 0.27 0.43 -0.28 -0.31 -0.49 -0.86 0.89
floor( v )
[1] -1 -1 0 0 1 -1 0 0 0 0 -1 -1 -1 -1 0
ceiling( v )
[1] -1 0 0 1 1 0 1 1 1 1 0 0 0 0 1
heights <- c( Amy = 166, Eve = 170, Bob = 177 )
heights
Amy Eve Bob
166 170 177
names( heights )
[1] "Amy" "Eve" "Bob"
names( heights ) <- c( "AMY", "EVE", "BOB" )
heights
AMY EVE BOB
166 170 177
heights[[ "EVE" ]]
[1] 170
expand_grid( x = c( 1:3, NA ), y = c( "a", "b" ) )
# A tibble: 8 x 2
x y
<int> <chr>
1 1 a
2 1 b
3 2 a
4 2 b
5 3 a
6 3 b
7 NA a
8 NA b
combn( c( "a", "b", "c", "d", "e" ), m = 2, simplify = TRUE )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a" "a" "a" "a" "b" "b" "b" "c" "c" "d"
[2,] "b" "c" "d" "e" "c" "d" "e" "d" "e" "e"
combn( c( "a", "b", "c", "d", "e" ), m = 3, simplify = TRUE )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10]
[1,] "a" "a" "a" "a" "a" "a" "b" "b" "b" "c"
[2,] "b" "b" "b" "c" "c" "d" "c" "c" "d" "d"
[3,] "c" "d" "e" "d" "e" "e" "d" "e" "e" "e"
Copyright © 2021 Biomedical Data Sciences (BDS) | LUMC